/**
 * Created by yiba0104 on 2017/5/9.
 */

var fs = require('fs');
var readLine = require('lei-stream').readLine;
var Array = require('node-array');
var mysql = require('mysql');

var connection = mysql.createConnection({
    host : 'yibasdkdb-1.cluster-cmd5cf3htxix.us-west-2.rds.amazonaws.com',
    port : 3306,
    user : 'yiba',
    password : 'TGB7ygv!QAZ',
    database : 'sdk',
    charset : 'UTF8_GENERAL_CI',
    debug : false
});

// 表名
var tableName = "package_country_num_test";

var arguments = process.argv.splice(2);
var files = arguments;
// 打印文件list
console.log('file: ', arguments);

console.log('处理开始');

var totalCount = {};

// 同步foreach循环
files.forEachAsync(function(file, index, arr, next1) {
    console.log(file);

    // 打印开始读文件的时间
    console.log('read start: ', new Date());

    // 成功条数计数器
    var i = 0;
    // 失败条数计数器
    var j = 0;

    // 读入错误输出文件
    var outputFile = './error/' + file;
    var output = fs.createWriteStream(outputFile);



    // 生产环境
    //readLine('./../s3/other-wifi-ec2-01/' + file).go(function (data, next) {
    // 测试环境
    readLine('./json/' + file).go(function (data, next2) {


        try {
            i++;
            var json = JSON.parse(data);

            var param = {
                date: json.createAt.s,
                country: json.country.s,
                city: json.city.s,
                packageName: json.currentPackageName.s,
                count: 1

            };


            if (totalCount[json.createAt.s + '_' + json.currentPackageName.s + '_' + json.country.s + '_' + json.city.s]) {
                totalCount[json.createAt.s + '_' + json.currentPackageName.s + '_' + json.country.s + '_' + json.city.s].count++;
            } else {
                totalCount[json.createAt.s + '_' + json.currentPackageName.s + '_' + json.country.s + '_' + json.city.s] = param;
            }

        }
        catch(e){
            output.write(j + ':\n' + data + '\n');
            j++;
        }

        next2();
    }, function () {
        console.log('read end: ', new Date());
        console.log('read success: ', i);
        console.log('read catch', j);

        console.log('-------------');
        next1();
    });

    return true;
}, function() {
    console.log('处理完成');

    var dataList = [];

    var k = 0;
    for (var param in totalCount) {
        k++;
        //if (k <= 200 && k > 0){
            dataList.push(totalCount[param]);
        //}
    }

    console.log("数据条数: ", dataList.length);
    console.log("存储开始");

    /*
    * 存储数据库，index条开始，存num条
    * */
    function saveMysql(index, num) {
        var query = '';
        // 边界值，跳出循环
        if (dataList[index]) {
            query = "INSERT INTO `" + tableName + "` (`creatDate`,`packageName`, `country`, `city`, `num`) VALUES (\"" + dataList[index].date + "\", \"" + dataList[index].packageName + "\", \"" + dataList[index].country + "\", \"" + dataList[index].city.replace(/"/g, "\\\"") + "\", " + dataList[index].count + ")";
        }

        for (var i = index + 1; i < index + num; i++) {

            // 边界值，跳出循环
            if (dataList[i]) {
                query += ", (\"" + dataList[i].date + "\", \"" + dataList[i].packageName + "\", \"" + dataList[i].country + "\", \"" + dataList[i].city.replace(/"/g, "\\\"") + "\", " + dataList[i].count + ")";
            } else {
                break;
            }
        }
        //console.log(query);
        if (query) {
            // 存储数据库
            connection.query(query, function(err, result) {
                if (err) {
                    console.log("存储失败：", index + '---' + (index + num) );
                    throw err;
                }
                console.log("存储成功：", index + '---' + (index + num) );
                if (index + num < dataList.length) {
                    saveMysql(index + num, num);
                } else {
                    connection.end();
                }

            });
        }
    }

    // 从第0条开始存储，每次春初2000条
    saveMysql(0, 2000);
});

//Poselok Turisticheskogo pansionata "Klyazminskoe vodohranilische"